package com.heer.gymnasium.module.coach.views;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.coach.beans.CoachBean;
import com.heer.gymnasium.module.coach.beans.CoachResponseBean;
import com.heer.gymnasium.module.main.adapters.HomeAdapter;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.util.StringUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 店铺私教列表
 * Created by zy on 2018/1/29.
 */

public class CoachListActivity extends BaseRefreshActivity {

    private static final int RES_ID_ROOT = R.layout.layout_coach_list;
    private static final int RES_ID_LIST = R.id.layout_coach_list_lv;
    private static final int RES_ID_LIST_ITEM = R.layout.item_coach;

    @BindView(RES_ID_LIST)
    ListView listView;

    private ArrayList<CoachBean> dataList;
    private QuickTypeAdapter<CoachBean> adapter;

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_ROOT);
        setRefreshResId(RES_ID_REFRESH_LAYOUT);
        dataList = new ArrayList<>();
        adapter = new QuickTypeAdapter<CoachBean>(this, dataList, RES_ID_LIST_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, CoachBean data, int type, int pos) {
                HomeAdapter.bindCoachItem(context, holder, data, pos);
            }
        };
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("场馆私教");
        titleUtils.startMainActivity(this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getCoachList();
        setRefresh(false);
    }

    private void setRefresh(boolean refresh) {
        if (layoutRefresh != null) {
            layoutRefresh.setRefreshing(refresh);
        }
    }

    private void getCoachList() {
        String storeId = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(storeId)) {
            ToastUtil.show(this, "店铺ID为空");
            finish();
            return;
        }
        mAppHelper.apiService().getCoachList("", storeId, "", pageNo + "", "", "", "").enqueue(new BaseCallback<ApiResponse<CoachResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CoachResponseBean>> call, Response<ApiResponse<CoachResponseBean>> response) {
                if (pageNo == 1) {
                    dataList.clear();
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.BOTH);
                }
                int total = StringUtils.toInt(response.body().getData().getTotal());
                int current = StringUtils.toInt(response.body().getData().getCurrent_page());
                int per_page = StringUtils.toInt(response.body().getData().getPer_page());
                if (total <= current * per_page) {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.TOP);
                }
                dataList.addAll(CoachBean.dataChange(response.body().getData().getData()));
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * @param context
     * @param id      场馆id
     */
    public static void startActivity(Context context, String id) {
        Intent mIntent = new Intent(context, CoachListActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        context.startActivity(mIntent);
    }
}
